$(document).ready(function() {
	dbDetail.init();
    dbDetail.getsourcelist();
});
var _mmg = null;
var editIndex = undefined;
var editRow=undefined;
var dbDetail = function(){
    return{
        init: function () {
            _mmg = $('#dg').datagrid({
                url: '/dbDetailPage',
                params: $("#dbDetailForm").serialize(),
                method: 'post',
                rownumbers: true,
                fitColumns: true,
                striped: true,
                nowrap: true,
                pageList: [25, 30, 35],
                idField: 'id', //主键
                onClickRow: 'onClickRow',
                columns: [
                    [
                        {field: 'ck', checkbox: true},
                        {field: 'id', title: '序号', width: 50},
                        {field: 'crawl_time', title: '数据节点时间', width: 50},
                        {field: 'capacity', title: '容量', width: 50},
                        {field: 'db_count', title: '数据条数', width: 50},
                        {field: 'all_time', title: '爬取耗时', width: 50},
                        {
                             field: 'see_db',
                             title: '操作',
                             width: 65,
                             type: 'String',
                             formatter: function (val, item, rowIndex) {
                                     return '<span><a href="#" onclick="dbConfig.see(\''+ item.id +'\')"><span style="color:;">删除</span></a></span>';
                             }
                             },
                    
                    ]
                ],
              


            }).datagrid("clientPaging");
        },
        
           //初始化网站下拉框
        getsourcelist: function () {
            $('#source_info').combobox(
                {
                    url: '/getsourcelist',
                    valueField: 'id',
                    textField: 'text',
                    method: 'POST'
                }
            )
            //      $.ajax({
            //         type: "post",
            //         url: '/getcountrylist',
            //         success: function (result) {
            //            
            //              var str={"firstName":"Brett"}
            //              $("#source_info").combobox('setValue', str['firstName']);
            //         }
            //     });
        },
         //编辑商品
        endEditing: function () {
            //开启编辑
            _mmg.datagrid("beginEdit", editIndex);

        },
          //查看节点设置
        see: function (id) {
            var  link='/dbDetail'
            window.location.href = link;
            
        },
          //保存设置
        save: function () {
              _mmg.datagrid('endEdit', editIndex);
              // _mmg.datagrid('acceptChanges');

              var updateRows = _mmg.datagrid('getChanges');
              console.info(updateRows)
              // var id=  updateRows[0]['id']
              // var star_time=  updateRows[0]['star_time']
              // var repetition_period=  updateRows[0]['repetition_period']
                $.messager.confirm('确认', '您确定要爬取该网站信息？', function (r) {
                if (r) {
                     $.ajax({
                            type: 'POST',
                            url: '/kl_start',
                            data: 'id=' + integral_id,
                            success: function (result) {
                                if (result == '1') {
                                    alert("爬取信息成功")
                                    dbDetail.load();
                                    return;
                                } else {
                                    alert('爬取信息失败！');
                                    return;
                                }
                            }
                        })
                     
                }
            });

        },
        //查询
        load: function () {
           $('#dg').datagrid('load',{
            });
        },
        //保存商品记录
        accept: function () {
            _mmg.datagrid('endEdit', editIndex);
            _mmg.datagrid('acceptChanges');

        },
         //打开弹出窗
        addPopu: function () {
           $('#w').window('open');
        },
         //关闭_弹出框
        closePopu: function () {
            $('#w').window('close', true);

        },

         //删除功能
       del: function () {
            var chackedItem = $('#dg').datagrid('getChecked');
            var integral_ids = [];
            var ids;
            if (chackedItem.length == '0') {
                alert("请至少选择一条记录！");
                return;
            }
            $.messager.confirm('确认', '您确定要删除当前选中的记录吗？', function (r) {
                if (r) {
                    if (chackedItem.length == 1) {
                        //删除单条记录
                        var chackedItem1 = $('#dg').datagrid('getChecked');
                        var integral_id;
                        $.each(chackedItem1, function (index, item) {
                            integral_id = item.id;
                        });
                        $.ajax({
                            type: 'POST',
                            url: '/del_Integral_by_id',
                            data: 'id=' + integral_id,
                            success: function (result) {
                                if (result == '1') {
                                    alert("删除成功")
                                    dbConfig.load();
                                    return;
                                } else {
                                    alert('删除失败！');
                                    return;
                                }
                            }
                        })
                    } else {
                        //删除多条记录
                        $.each(chackedItem, function (index, item) {
                            if (integral_ids == "") {
                                integral_ids = "" + item.id + "";
                                
                            } else {
                                integral_ids = "" + item.id + "" + "," + integral_ids;
                                ids = integral_ids.split(',');
                            }
                        });
                        $.ajax({
                            type: 'POST',
                            url: '/del_Integral_by_ids',
                            data: 'id=' + JSON.stringify(ids),
                            success: function (result) {
                                if (result == '1') {
                                    alert('删除成功！');
                                    proInfoQuery.load();
                                    return;
                                } else {
                                    alert('删除失败！');
                                    return;
                                }
                            }
                        })
                    }
                }
            });
        },

       //清空表单
        clearForm: function (formID, unload) {
            $("#" + formID).each(function () {
                $("#jd_name").val('')
                $("#first_price").val('')
                $("#last_price").val('')
                $("#firsttime").datetimebox('clear');
                $("#lasttime").datetimebox('clear');
                $('#source_info').combobox('clear');
                // iterate the elements within the form
                $(':input', this).each(function () {
                    var type = this.type, tag = this.tagName.toLowerCase();
                    if (type == 'text' || type == 'password' || tag == 'textarea' || type == "file")
                        this.value = '';
                    else if (type == 'checkbox' || type == 'radio')
                        this.checked = false;
                    else if (tag == 'select')
                        $('#source_info').combobox('clear');
                });
                $('#dg').datagrid('load', {});
            });
        }

    }

}
();

